﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;

namespace LabyCLI.Commands
{
	class cmd_move
    {
        Terminal frm = Terminal.Instance;

        public void Main(List<string> args)
        {
            if (File.Exists(args[0])/* && System.IO.Directory.Exists(args[1])*/)
            {
				Directory.Move(args[0], args[1]);
                Terminal.WriteLine("Moved the file '" + args[0] + "' to the directory '" + args[1] + "'." + Environment.NewLine);
            }
            else
            {
                Terminal.WriteLine("Invaild arguments." + Environment.NewLine, Properties.Settings.Default.Terminal_ErrorForeColor);
            }
        }

        public void Main()
        {
            Terminal.WriteLine("Invaild amount of arguments." + Environment.NewLine, Properties.Settings.Default.Terminal_ErrorForeColor);
        }

		public static void MoveDirectory(string source, string target)
		{
			var sourcePath = source.TrimEnd('\\', ' ');
			var targetPath = target.TrimEnd('\\', ' ');
			var files = Directory.EnumerateFiles(sourcePath, "*", SearchOption.AllDirectories)
								 .GroupBy(s => Path.GetDirectoryName(s));

			foreach (var folder in files)
			{
				var targetFolder = folder.Key.Replace(sourcePath, targetPath);
				Directory.CreateDirectory(targetFolder);
				foreach (var file in folder)
				{
					var targetFile = Path.Combine(targetFolder, System.IO.Path.GetFileName(file));
					if (File.Exists(targetFile))
						File.Delete(targetFile);
					File.Move(file, targetFile);
				}
			}

			Directory.Delete(source, true);
		}
    }
}
